package com.demo.youxuanmall.controller.admin;

import com.demo.youxuanmall.common.Result;
import com.demo.youxuanmall.controller.BaseController;
import com.demo.youxuanmall.entity.User;
import com.demo.youxuanmall.service.SystemSettingService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Map;

@Slf4j
@Controller
@RequestMapping("/admin/settings")
public class AdminSettingController extends BaseController {

    public AdminSettingController(SystemSettingService systemSettingService) {
        super(systemSettingService);
    }

    /**
     * 系统设置页面
     */
    @GetMapping
    public String settingsPage(HttpSession session, Model model) {
        // 验证管理员权限
        User user = (User) session.getAttribute("user");
        if (user == null || user.getRole() != 1) {
            return "redirect:/403";
        }
        
        // 获取所有设置
        Result<Map<String, Map<String, Object>>> result = systemSettingService.getAllSettings();
        if (result.getCode() == 0 && result.getData() != null) {
            model.addAttribute("settings", result.getData());
        }
        
        return "admin/settings/index";
    }
    
    /**
     * 更新基本设置
     */
    @PostMapping("/basic")
    @ResponseBody
    public Result<Boolean> updateBasicSettings(@RequestBody Map<String, String> settings, HttpSession session) {
        // 验证管理员权限
        User user = (User) session.getAttribute("user");
        if (user == null || user.getRole() != 1) {
            return Result.error("无权限操作");
        }
        
        return systemSettingService.updateSettings(settings);
    }
    
    /**
     * 更新安全设置
     */
    @PostMapping("/security")
    @ResponseBody
    public Result<Boolean> updateSecuritySettings(@RequestBody Map<String, String> settings, HttpSession session) {
        // 验证管理员权限
        User user = (User) session.getAttribute("user");
        if (user == null || user.getRole() != 1) {
            return Result.error("无权限操作");
        }
        
        return systemSettingService.updateSettings(settings);
    }
    
    /**
     * 更新邮件设置
     */
    @PostMapping("/email")
    @ResponseBody
    public Result<Boolean> updateEmailSettings(@RequestBody Map<String, String> settings, HttpSession session) {
        // 验证管理员权限
        User user = (User) session.getAttribute("user");
        if (user == null || user.getRole() != 1) {
            return Result.error("无权限操作");
        }
        
        return systemSettingService.updateSettings(settings);
    }
    
    /**
     * 清除缓存
     */
    @PostMapping("/clear-cache")
    @ResponseBody
    public Result<Boolean> clearCache(HttpSession session) {
        // 验证管理员权限
        User user = (User) session.getAttribute("user");
        if (user == null || user.getRole() != 1) {
            return Result.error("无权限操作");
        }
        
        return systemSettingService.clearCache();
    }
} 